package com.turtle.extend;

import com.turtle.dto.BlogSortInfoDto;
import com.turtle.dto.BlogSortListDto;
import com.turtle.dto.BlogSortParam;
import com.turtle.entity.sql.BlogSort;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lijiayu
 * @date 2020/7/21
 * @description
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface BlogSortConverter {
    BlogSortConverter INSTANCE = Mappers.getMapper(BlogSortConverter.class);

    BlogSort toEntity(BlogSortParam param, Long userId);

    BlogSortInfoDto toDto(BlogSort blogSort);

    default List<BlogSortListDto> toListDto(List<BlogSort> list, Map<Long, Long> countMap) {
        if (CollectionUtils.isEmpty(list)) {
            return List.of();
        }
        return list.stream().map(x -> BlogSortListDto.builder()
                .id(x.getId())
                .name(x.getName())
                .picUrl(x.getPicUrl())
                .status(x.getStatus())
                .articleCount(countMap.get(x.getId()))
                .build())
            .collect(Collectors.toList());
    }
}
